package pw.hais.main.main_app.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.avos.avoscloud.AVAnalytics;

import pw.hais.main.R;
import pw.hais.main.main_app.app.BaseActivity;
import pw.hais.main.main_app.app.V;
import pw.hais.main.main_app.model.PushServiceModel;
import pw.hais.utils.EmptyUtil;
import pw.hais.utils.L;
import pw.hais.utils.SPUtils;

/**
 *
 * 接收推送信息
 *
 * 推送格式
 *  {
 *     "title":"百度",
 *    "content":"打开百度",
 *    "url":"http://m.baidu.com"
 *    }
 * Created by Hais1992 on 2015/12/14.
 */
public class PushServiceActivity extends BaseActivity implements View.OnClickListener {
    private TextView text_title, text_content;
    private Button btn_ok;
    private PushServiceModel pushInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pushservice);

        String mssage;

        if(getIntent().getExtras()!=null){
            mssage = getIntent().getExtras().getString("com.avos.avoscloud.Data");
            SPUtils.saveObject(V.SP.PUSH_MESSAGE_KEY,mssage);
        }else{
            mssage = SPUtils.getObject(V.SP.PUSH_MESSAGE_KEY);
            if(mssage!=null){
                L.showShort("这是上次收到消息的存档...");
            }
        }

        L.i("收到推送："+mssage);
        pushInfo = gson.fromJson(mssage, PushServiceModel.class);
        if (pushInfo == null){
            finish();
            L.showShort("暂时没有收到消息...");
        }
        else {
            initLayoutView();
        }
    }

    private void initLayoutView() {
        text_title = (TextView) findViewById(R.id.text_title);
        text_content = (TextView) findViewById(R.id.text_content);
        btn_ok = (Button) findViewById(R.id.btn_ok);

        if(!EmptyUtil.emptyOfString(pushInfo.file_url)){
            pushInfo.url = pushInfo.file_url;
            btn_ok.setText("立即下载");
        }

        text_title.setText(pushInfo.title+"");
        text_content.setText(pushInfo.content+"");
        text_content.setOnClickListener(this);
        btn_ok.setOnClickListener(this);

    }


    @Override
    protected void onStart() {
        super.onStart();
        AVAnalytics.trackAppOpened(getIntent());
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(context,WebActivity.class);
        intent.putExtra("OPEN_URL",""+pushInfo.url);
        startActivity(intent);
    }
}
